/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "FileBrowser.h"

FileBrowser::FileBrowser(QString defDir, QWidget* parent) : ui::OnyxDialog(parent)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    updateTitle(tr("打开文件"));

    m_vlayout = new QVBoxLayout(this);
    content_widget_.setLayout(m_vlayout);

    m_model = new QStandardItemModel(this);
    m_treeView = new ui::OnyxTreeView(this, m_model);
    m_treeView->showHeader(false);
    m_vlayout->addWidget(m_treeView);
    connect(m_treeView, SIGNAL(activated(const QModelIndex&)), this, SLOT(selectedItem(const QModelIndex&)));

    m_start = true;
    entryDir(defDir);

    m_timer.setSingleShot(true);
    m_timer.start(100);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(update()));

    Out_Log(__FUNCTION__, COCLASSNAME);
}

bool FileBrowser::event(QEvent* e)
{
    switch (e->type())
    {
    case QEvent::KeyRelease:
        return true;
    case QEvent::KeyPress:
        {
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
            switch (keyEvent->key())
            {
            case Qt::Key_Escape:
                {
                    entryDir(m_dir.absoluteFilePath(tr("..")));
                    return true;
                }
                break;
            case Qt::Key_Menu:
                {
                    reject();
                }
                break;
            default:
                {
                    m_treeView->keyReleaseEvent(keyEvent);
                    return true;
                }
                break;
            }
        }
        break;
    default:
        break;
    }

    bool ret = ui::OnyxDialog::event(e);

    switch (e->type())
    {
    case QEvent::UpdateRequest:
        {
            refreshScreen();
            return true;
        }
        break;
    default:
        {

        }
        break;
    }
    return ret;
}

void FileBrowser::refreshScreen(ScreenProxy::Waveform w, bool whole)
{
    ScreenProxy::instance().updateWidget(this, w, whole);
}

QString FileBrowser::fileBrowse(QString defDir, QWidget* parent)
{
    FileBrowser dlg(defDir, parent);

#ifdef QT_ARCH_ARM
    dlg.showFullScreen();
#else
    if (parent != 0)
    {
        dlg.setFixedSize(parent->size());
    }
    else
    {
        dlg.setFixedSize(600, 800);
    }
#endif

    if (dlg.exec() == Accepted)
    {
        return dlg.m_fileName;
    }

    return QString();
}

void FileBrowser::entryDir(const QString strDir)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_dir.setPath(QDir::cleanPath(strDir));

    if (!m_dir.exists())
    {
        m_dir.setPath(tr("/media"));
    }

    // set model of the onyxtreeview
    QStringList names = m_dir.entryList(QStringList()<<"*.zip");
    names += m_dir.entryList(QDir::Dirs).mid(2);
    names.prepend("..");
    names.prepend(".");

    m_model->clear();
    for (int i = 0; i < names.count(); i++)
    {
        QStandardItem* item = new QStandardItem(names.at(i));
        m_model->appendRow(item);
    }

    m_treeView->setModel(m_model);
    m_treeView->jumpToPage(1);

    QStringList picNames = m_dir.entryList(QStringList()<<"*.jpg"<<"*.png", QDir::NoFilter, QDir::Name | QDir::IgnoreCase);

    if (!m_start && !picNames.isEmpty())
    {
        ui::MessageDialog dlg(QMessageBox::Question, tr("询问"), tr("当前目录下找到图片") + picNames[0] + tr("，是否载入？"), QMessageBox::Yes | QMessageBox::No);
        if (dlg.exec() == QMessageBox::Yes)
        {
            m_fileName = m_dir.absolutePath();
            accept();
        }
        update();
    }

    m_start = false;

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void FileBrowser::selectedItem(const QModelIndex& index)
{
    QString fileName = ((QStandardItemModel*)index.model())->itemFromIndex(index)->text();

    if (fileName.endsWith(".zip", Qt::CaseInsensitive))
    {
        m_fileName = m_dir.absoluteFilePath(fileName);
        accept();
        return;
    }

    entryDir(m_dir.absoluteFilePath(fileName));
}

